package richbar.com.richbar.activity.me;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.common.base.BaseActivity;
import com.common.util.StringUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;
import java.util.Arrays;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.linear.LinearLayoutAdapter;
import richbar.com.richbar.view.linear.LinearView;

/**
 * Created by QiYang on 2018/11/16.
 */

public class AccountSetAc extends BaseActivity {
    @BindView(R.id.head_iv)
    ImageView headIv;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.linearView)
    LinearView linearView;
    private MyListAdapter adapter;
    UserInfoResult.DataEntity.InfoEntity bean;
    private LoadingDialog loadingDialog;
    @Override
    public int initLayout() {
        return R.layout.ac_account_set;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("账户设置");
    }

    @Override
    public void initView() {
        loadingDialog = new LoadingDialog(this,0);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        getUserInfo();
        String[] s = {"收货地址","实名认证","账户安全","支付设置","关联账户","关于APP"};
        adapter = new MyListAdapter(this, Arrays.asList(s));
        linearView.setAdapter(adapter);
        linearView.setOnItemClickListener(new LinearView.OnItemClickListener() {
            @Override
            public void onItemClicked(View v, int position) {
                switch (position){
                    case 0:
                        start(ManageGoodsAddressAc.class);
                        break;
                    case 1:
                        start(RealNameAuthenticationAc.class);
                        break;
                    case 2:
                        start(AccountSecurityAc.class);
                        break;
                    case 3:
                        start(PaySetAc.class);
                        break;
                    case 4:
                        start(AccountRelationAc.class);
                        break;
                    case 5:
                        start(AboutAppAc.class);
                        break;
                }
            }
        });
    }

    private void getUserInfo() {
        loadingDialog.show();
        NetWorkUtil.getInfo(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final UserInfoResult result = MyGson.getInstance().fromJson(data, UserInfoResult.class);
                if (checkIsOk(result)){
                    setUserInfo(result);
                    loadingDialog.dismiss();
                }
            }
        });
    }

    private void setUserInfo(UserInfoResult result) {
        bean = result.getData().getInfo();
        GlideUtil.loadCircleImage(this,bean.getHead(),headIv);
        nickname.setText(StringUtil.isEmpty(bean.getNickname()) ? "" : bean.getNickname());
        username.setText("用户名：" + bean.getUsername());
    }


    @OnClick(R.id.user_body)
    public void onViewClicked() {
        startForResult(PersonInfoAc.class, 101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK){
            getUserInfo();
            Intent intent = new Intent();
            setResult(RESULT_OK,intent);
        }
    }

    class MyListAdapter extends LinearLayoutAdapter<String> {

        public MyListAdapter(Context context, List<String> list) {
            super(context, list);
        }

        @Override
        protected View getView(List<String> data, String bean, int position) {
            View view = LayoutInflater.from(AccountSetAc.this).inflate(R.layout.item_account_set,null);
            TextView title = view.findViewById(R.id.title);
            title.setText(bean);
            return view;
        }
    }
}
